package com.example.concurrentprogramming8016.thread;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author liyanhao
 * @Date 2022-12-25 14:41
 */
public class ThreadTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        extracted();
//        extracted1();
//        new Thread(()->System.out.println(Thread.currentThread().getName())).start();
//线程优先级


    }

    private static void extracted1() throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Future<Integer> submit = executorService.submit(new CallableTask());
        Integer integer = submit.get();
        System.out.println(integer);
        executorService.shutdown();
    }

    private static void extracted() throws InterruptedException {
        final ArrayList<Integer> list = new ArrayList<>();
        final Random random = new Random();
        Thread thread = null;
        for (int i = 0; i < 10000; i++) {
            thread = new Thread(() -> {
                list.add(random.nextInt());
            });
            thread.start();
            //添加这个会使程序变成串行的
            thread.join();
            System.out.println(list.size());
        }
    }
}

class CallableTask implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        return new Random().nextInt();
    }
}

